package com.ciaojian.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ciaojian.core.model.MyCoupon;
import com.ciaojian.core.model.vo.CouponVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author ${author}
 * @since 2020-09-12
 */
@Mapper
public interface MyCouponMapper extends BaseMapper<MyCoupon> {

    /**
     * 获取title
     *
     * @return title
     */
    @Select("SELECT GROUP_CONCAT(dhs.title) FROM d_member dm LEFT JOIN d_haircut_service dhs ON FIND_IN_SET(dhs.id,dm.service)")
    String getTitles();

    /**
     * 通过 Code 查询 Coupon
     *
     * @param code 优惠券码
     * @return CouponVo
     */
    CouponVo getCouponByCode(String code);

    /**
     * 根据用户 id 获取用户获取会员的时间
     *
     * @param userId 用户 id
     * @return 用户获取会员的时间
     */
    @Select("SELECT update_time FROM d_user_member WHERE user_id = #{userId} AND member_id = 1")
    Date getMemberDate(Integer userId);

    /**
     * 查询购买会员后是否是首次剪发
     *
     * @param userId 用户 id
     * @param date   日期
     * @return 结果
     */
    @Select("SELECT count(1) FROM d_order WHERE user_id = #{userId} AND pay_time > #{date} AND `status` >= 3")
    Integer getFirstOrder(Integer userId, Date date);

    /**
     * 使用优惠券
     *
     * @param orderNo   订单号
     * @param reduction 优惠价格
     * @param code      优惠券码
     */
    void useCoupon(String orderNo, BigDecimal reduction, String code);

    /**
     * d
     *
     * @param userId 用户 id
     * @return 数量
     */
    @Select("SELECT count(1) FROM d_user_member WHERE user_id = #{userId} AND due_date > NOW()")
    Integer getUserByMember(Integer userId);

    /**
     * 获取服务类型
     *
     * @return 服务类型
     */
    @Select("SELECT all_service FROM d_member LIMIT 0,1")
    Integer getMemBerServiceStatus();

    /**
     * 获取会员优惠折扣
     *
     * @return 折扣
     */
    @Select("SELECT discount FROM d_member LIMIT 0,1")
    BigDecimal getDiscount();

    void updateCodeStatus(String code, Integer status);

    @Select("SELECT `status` FROM d_order WHERE order_no = #{orderNo}")
    Integer getOrderStatus(String orderNo);

    /**
     * @param userId
     * @return
     */
    @Select("SELECT count(1) FROM d_user_member WHERE user_id = #{userId} AND due_date > to_days(now()) AND member_id = #{memberId}")
    Integer getMemBerCount(String userId, Integer memberId);
}
